package game.editer.view;

import game.editer.ui.IAttributeInput;
import game.editer.ui.InputCreater;
import game.editer.ui.BaseAttributeInput;
import hx.layout.AnchorLayoutData;
import hx.display.VBox;
import game.editer.AttrEditer;

using Reflect;

/**
 * 输入列表数据
 */
class InputListDataView extends AlertView {
	/**
	 * 所有输入源
	 */
	public var inputs:Array<BaseAttributeInput> = [];

	public var attrs:Array<AttrData> = [];

	public function new(title:String, attr:Array<AttrData>, width:Int = 300, height:Int = 400) {
		super(title, width, height);
		this.attrs = attr;
		var vbox = new VBox();
		this.addChild(vbox);
		vbox.layoutData = new AnchorLayoutData(36, 30, 30, 30);
		for (item in attr) {
			var input = InputCreater.create(item, onAttributeChange);
			vbox.addChild(input);
			inputs.push(input);
		}
		this.buttons = ["确定", "取消"];
	}

	private function onAttributeChange(attr:IAttributeInput):Void {}

	/**
	 * 设置编辑的数据源
	 */
	public var data(get, set):Dynamic;

	private var __data:Dynamic = null;

	private function get_data():Dynamic {
		return __data;
	}

	private function set_data(data:Dynamic):Dynamic {
		__data = data;
		for (i => input in this.inputs) {
			input.value = data.getProperty(input.key) ?? attrs[i].getDefaultValue();
		}
		return __data;
	}

	public function done():Dynamic {
		if (__data == null)
			return __data;
		for (input in this.inputs) {
			__data.setProperty(input.key, input.value);
		}
		return __data;
	}
}
